/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package geneproteinlinking;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author nadaalhirabi
 */
public class GeneProteinLinking {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws FileNotFoundException, IOException {
        // TODO code application logic here
        
        String Gff_file_name, Dom_file_name, Complete_file;
        Scanner User_input = new Scanner(System.in);
        
        System.out.print("Enter the Gff file name:");
        Gff_file_name = User_input.next(); 
        
        System.out.print("Enter the domain file name:");
        Dom_file_name = User_input.next(); 
        
        
        //---------------- read domain file
        
     String domtoken = "";    
     List<String> Domtemps;
     int domlength=0;
        try (Scanner inFile = new Scanner(new File(Dom_file_name)).useDelimiter("\n")) {
            Domtemps = new LinkedList<>();
            while (inFile.hasNext()) {
                // find next line
                domtoken = inFile.next();
                // System.out.print(token1);
                // System.out.println();             
                Domtemps.add(domtoken);
                domlength++;
            }   }

    String[] tempsDomArray = Domtemps.toArray(new String[0]); 
    String[] Domdigested = new String[domlength];
    
     int b=0;
        for (String st : tempsDomArray) {//llop for all the file// ecopy to array
        Domdigested[b++]=st;
        }
    ////------------////
    
    //---------- read the gff file ------------    
     String token1 = "";    
     List<String> temps;
     int gfflength=0;
        try (Scanner inFile1 = new Scanner(new File(Gff_file_name)).useDelimiter("\n")) {
            temps = new LinkedList<>();
            while (inFile1.hasNext()) {
                // find next line
                token1 = inFile1.next();       
                temps.add(token1);
                gfflength++;
                
            }   }
    String[] tempsArray = temps.toArray(new String[0]);
    String[] gffdigested = new String[gfflength];// the length because it will hold the domain info too
    
        int a=0;
        for (String s : tempsArray) {//loop for all the file// ecopy to array
        gffdigested[a++]=s;
        }
///------end Gff reading -----------//
    
// loop through gff array to find the connected protein ///
int j=0;
String[] complete_digested = new String[gfflength+domlength];//  because it will hold the domain info too

for ( int i=0; i<gffdigested.length ;++i) {  
//System.out.println(gffdigested[1]);  

String[] parts=gffdigested[i].split("\t");
String type=parts[0];//try to find gene type only
System.out.println(type);

if(type.equals("gene")){//if it is gene we do loop on the domain array to match them
String ID=parts[3];//to compare the ID with dom id
complete_digested[j++]=gffdigested[i];//add gene

int found=0;
boolean firstmismatch=false;
    for(int x=0; x<Domdigested.length  && !(firstmismatch); ++x)     {
        String[] str=Domdigested[x].split("\t");
        String DomId =str[0]; 
        
        if(ID.equals(DomId)){//first found to the domain
            complete_digested[j++]="Dom"+"\t"+Domdigested[x];//add the dom info after gene tyb
            found++;// if found = 10 exit loop // we assume the max domains for one protein is 7
            
            if( (x+1)<Domdigested.length ){//see if the next domain is belong to this gene// we do not need to complete searching
                String[] test=Domdigested[x+1].split("\t");
                String testID =test[0];  
                if(ID.equals(testID)== false) firstmismatch=true;// that means the next domain is not belong to this gene
              }
        }   
    }//end loop on domain     
}
else complete_digested[j++]=gffdigested[i];//add gene other parts : exone ....;//copy
    
}//end loop on gff array
        
   writeTofile(complete_digested, "FeaturesLinkedFile");
         
    }//end main
    
    
public static void writeTofile(String[] digestedfile,  String filename) throws FileNotFoundException, IOException{
BufferedWriter writer = null;
try {
    File file = new File(filename+".txt");  
    PrintWriter out = new PrintWriter(new FileWriter(file));  

    // Write each string in the array on a separate line  
    for ( int i=0; i<digestedfile.length ;++i) {  
        String s =digestedfile[i];
        out.println(s);  
       //if(s.equals("##END"))break;
}    
out.close();  

    } catch(IOException ex) {
        ex.printStackTrace();
    }  
   
}//end write to file
    
}
